package com.sharepics.dao;

import java.util.Calendar;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;
import com.sharepics.bean.Album;
import com.sharepics.bean.Picture;

@Repository
public class PictureDAO {
	static {
        ObjectifyService.register(Picture.class);
    }
	
	
	public PictureDAO(){
		
	}
	
    public List<Picture> findAll() {
        Objectify ofy = ObjectifyService.begin();
        Query<Picture> q = ofy.query(Picture.class);
        return q.list();
    }
    
	
    public List<Picture> findByAlbum(Long albumId) {
        Objectify ofy = ObjectifyService.begin();
        Query<Picture> q = ofy.query(Picture.class).filter("albumId", albumId);
        return q.list();
    }

    public Picture get(long id) {
        Objectify ofy = ObjectifyService.begin();
        return ofy.find(Picture.class, (long) id);
    }

    public Key<Picture> save(Picture picture) {
        Objectify ofy = ObjectifyService.begin();
        picture.id = null;
        picture.creationDate = Calendar.getInstance().getTime();
        return ofy.put(picture);
       
    }

}
